package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.pojo.Announcement;
import com.jiyun.pojo.Carorder;
import com.jiyun.pojo.ResultVo;
import com.jiyun.service.CarorderService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

@RestController
@RequestMapping("/api/carorder")
public class CarorderController {

    @Autowired
    private CarorderService carorderService;

    @Autowired
    private RedisTemplate redisTemplate;
//    添加数据
    @PostMapping("/save")
    public ResultVo save(@RequestBody Carorder carorder){
        carorder.setCreateTime(new Timestamp(System.currentTimeMillis()));
//        生成随机数;
        carorder.setPicture(redisTemplate.opsForValue().get("fileUrl").toString());
        carorderService.save(carorder);

        return ResultVo.ok();
    }
//    判断重复
    @GetMapping("/findByOrderNumber/{orderNumber}")
    public ResultVo findByOrderNumber(@PathVariable("orderNumber") String orderNumber){
        QueryWrapper<Carorder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        Carorder one = carorderService.getOne(wrapper);
        System.out.println(one+"one");
        if (one !=null){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }
    @RequestMapping("/findCarOrderAll")
    public R findCarOrderAll(){
        return carorderService.findCarOrderAll();
    }

    @RequestMapping("/findCarOrderById")
    public R findCarOrderById(Integer id){
        return carorderService.findCarOrderById(id);
    }
}
